package com.sskj.one.mine;

import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/04
 */
public class NickNameActivity extends BaseActivity<NickNamePresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.et_nick_name)
    EditText etNickName;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_nick_name;
    }

    @Override
    public NickNamePresenter getPresenter() {
        return new NickNamePresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, new Observer<List<UserData>>() {
            @Override
            public void onChanged(@Nullable List<UserData> users) {
                if (users != null && users.size() > 0) {
                    userData = users.get(0);
                    if (isEmpty(userData.getNickname())) {
                        tvTitle.setText("设置昵称");
                    } else {
                        tvTitle.setText("编辑昵称");
                    }
                } else {
                    tvTitle.setText("设置昵称");
                }

            }
        });
    }

    @Override
    public void initData() {
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(etNickName)) {
                toastHint(etNickName);
                return;
            }
            mPresenter.setNickName(getText(etNickName));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NickNameActivity.class);
        context.startActivity(intent);
    }

    public void setNickNameOK() {
        finish();
    }
}
